<?php
class ModelXController extends SeatAppController {
	
	public function admin_index($venue_id = 1){
		$modelxs = $this->ModelX->find('all',array(
			'conditions'=>array(
				'ModelX.relate_id'=>$venue_id
			)
		));
		$this->set(compact('modelxs','venue_id'));
	}
	
	public function admin_add($relate_id = false) {
		if ($this->request->is('post')) {
			$this->ModelX->create();
			if ($this->ModelX->save($this->request->data)) {
				$this->Session->setFlash(__('The article has been saved.'));
				return $this->redirect(array('action' => 'index',$relate_id));
			} else {
				$this->Session->setFlash(__('The article could not be saved. Please, try again.'));
			}
		}
		$this->set(compact('relate_id'));
	}
	
	public function admin_config($id = false) {
		$data = $this->ModelX->findById($id);
		$this->set(compact('relate_id','data'));
	}

	public function admin_table($table_id,$seat = true){
		 if($seat){
		 	$recursive = 2;
		 }else{
		 	$recursive = 1;
		 }
	     $table = $this->ModelX->ModelTable->find('first',array(
	     	'conditions'=>array('ModelTable.id'=>$table_id),
	     	'recursive'=>$recursive
	     ));
	     $this->set(compact('table'));
	}
	
	public function admin_addTable($model_id = false){
	    if ($this->request->is('post')) {
		    if($this->ModelX->ModelTable->save($this->request->data)){
		        $state = true;
		    }else{
		        $state = false;
		    }
		    $this->set('state',$state);
	    }
	    $this->set('model_id',$model_id);
	}
	
	public function admin_editTable($table_id){
		if ($this->request->is(array('post','put'))) {
			$this->ModelX->ModelTable->id = $table_id;
			if ($this->ModelX->ModelTable->save($this->request->data)) {
				$state = true;
			}else{
				$state = false;
			}
			$this->set('state',$state);
		}else{
			$this->request->data = $this->ModelX->ModelTable->findById($table_id);
		}
	}
	
	public function admin_addTr($table_id){
	    $this->loadModel('Seat.ModelTr');
	    if ($this->request->is('post')) {
		    if($this->ModelTr->save($this->request->data)){
		        $state = true;
		    }else{
		        $state = false;
		    }
		    $this->set('state',$state);
	    }
	    $this->set('table_id',$table_id);
	}
	
	public function admin_editTr($tr){
	    $this->loadModel('Seat.ModelTr');
	    if ($this->request->is(array('post','put'))) {
	        $this->ModelTr->id = $tr;
	        if ($this->ModelTr->save($this->request->data)) {
	            $state = true;
	        }else{
	            $state = false;
	        }
	        $this->set('state',$state);
	    }else{
	        $this->request->data = $this->ModelTr->findById($tr);
	    }
	}
	
	public function admin_delTr($tr){
	    $this->loadModel('Seat.ModelTr');
	    $this->ModelTr->id = $tr;
	    $count = $this->ModelTr->ModelTd->find('count',array(
	    	'conditions'=>array(
	    		'ModelTd.tr_id'=>$tr
	    	)
	    ));
	    if($count>0){
	    	$state = false;
	    	$msg = '行下有定义的座位 请先删除座位';
	    }else if($this->ModelTr->delete()){
	        $state = true;
	    }else{
	        $state = false;
	    }
	    $this->set(compact('state','msg'));
	    $this->render(false);
	}
	
	public function admin_addTd(){
		$this->loadModel('Seat.ModelTd');
		$checker = $this->ModelTd->find('first',array(
			'conditions'=>array(
				'ModelTd.tr_id'=>$this->request->data['tr_id'],
				'ModelTd.y'=>$this->request->data['y']
			)
		));
		if($checker){
			$this->ModelTd->id = $checker['ModelTd']['id'];
		}
	    if($this->ModelTd->save($this->request->data)){
	    	$state = true;
	    }else{
	    	$state = false;
	    }
	    $this->set('state',$state);
	    $this->render(false);
	}
	
	public function admin_editTd($td){
	    $this->loadModel('Seat.ModelTd');
	    if ($this->request->is(array('post','put'))) {
	        $this->ModelTd->id = $td;
	        if ($this->ModelTd->save($this->request->data)) {
	            $state = true;
            }else{
                $state = false;      
            }
            $this->set('state',$state);
	    }else{
    	    $this->request->data = $this->ModelTd->findById($td);
	    }
	}
	
	public function admin_delTd($td){
	    $this->loadModel('Seat.ModelTd');
        $this->ModelTd->id = $td;
        if($this->ModelTd->delete()){
            $state = true;
        }else{
            $state = false;
        }
        $this->set('state',$state);
        $this->render(false);
	}
	
}